/**
 * 
 */
package creacionCuad;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import tad.Cell;

/**
 * @author Elena
 *
 */
public class creacionCuadrantes {
	
	private Color[][] rgbs;
	private boolean[][] map;
	private int w;
	private int h;	
	private Cell[][] celdas;
	
	private ArrayList<Cell> c;
	
	public creacionCuadrantes(int numC){
		File inputFile = new File("mapaC.png");
		BufferedImage bufferedImage;
		try {
			bufferedImage = ImageIO.read(inputFile);
		w = bufferedImage.getWidth();
		h = bufferedImage.getHeight();
		rgbs = new Color[w][h];
		map = new boolean[w][h];
		celdas = new Cell[w][h];
		for(int i=0;i<w;i++){
			numC++;
			for (int j=0;j<h;j++){
				int c=bufferedImage.getRGB(i, j);
				int  red = (c & 0x00ff0000) >> 16;
				int  green = (c & 0x0000ff00) >> 8;
				int  blue = c & 0x000000ff;
				// and the Java Color is ...
				Color color = new Color(red,green,blue);
				rgbs[i][j]=color;
				if(getColor(i, j)=='r')
					writePoint(numC,i,j);
			}
		}
		} catch (IOException e) {
			e.printStackTrace();
		}
		

	}
	
	private void writePoint(int numC,int i, int j){
		String fileConfig = "cuadrantes.txt";
		try{
			BufferedWriter bw = new BufferedWriter(new FileWriter(fileConfig,true));
			PrintWriter salida = new PrintWriter(bw);
			salida.println(numC+" "+i+" "+j);
			salida.close();
		}
		catch(java.io.IOException e){e.printStackTrace();}
	}
	
	/**
	 * 
	 * @param x width of rgbs
	 * @param y Height of rgbs
	 * @return 'w' for white, 'g' for green, 'b' for black
	 */
	public char getColor(int x, int y){
		Color c = rgbs[x][y];
		char col;
		//Blanco:
		if(c.getRed()>220 && c.getGreen()>220 && c.getBlue()>220)
			col='w';
		//Negro
		else if(c.getRed()<150 && c.getGreen()<150 && c.getBlue()<150)
			col = 'b';
		else if (c.getRed()<=200 && c.getGreen()>=130 && c.getBlue()>=180)//azul
			col = 'g';
		//rojo
		else if (c.getRed()>240 && c.getGreen()<10 && c.getBlue()<10)
			col = 'r';
		else col = 'X';
		return col;
	}
	
	public static void main(String[] args){
		creacionCuadrantes c = new creacionCuadrantes(1);
	}

}
